package service

import (
	"context"

	"github.com/go-kratos/kratos/v2/errors"
	"github.com/go-kratos/kratos/v2/log"

	pb "valuation-srv/api/valuation"
	"valuation-srv/internal/biz"
)

type ValuationService struct {
	pb.UnimplementedValuationServer
	logger *log.Helper
	vb     *biz.ValuationBiz
}

func NewValuationService(vb *biz.ValuationBiz, logger log.Logger) *ValuationService {
	return &ValuationService{vb: vb, logger: log.NewHelper(logger)}
}

func (s *ValuationService) GetEstimatePrice(ctx context.Context, req *pb.GetEstimatePriceReq) (*pb.GetEstimatePriceReply, error) {
	// 距离时长
	distance, duration, err := s.vb.GetDrivingInfo(ctx, req.Origin, req.Destination)
	if err != nil {
		return nil, errors.New(200, "MAP ERROR", "get driving info error")
	}
	// 费用
	price, err := s.vb.GetPrice(ctx, distance, duration, 1, 9)
	if err != nil {
		return nil, errors.New(200, "PRICE ERROR", "calculate price error")
	}

	return &pb.GetEstimatePriceReply{
		Origin:      req.Origin,
		Destination: req.Destination,
		Price:       price,
	}, nil
}
